﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CayleyTree
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (graphics == null) graphics = this.CreateGraphics();
            drawCayleyTree(n, 200, 310, leng, -Math.PI / 2);
        }

        private Graphics graphics;
        double th1 = 30 * Math.PI / 180;
        double th2 = 20 * Math.PI / 180;
        double per1 = 0.6;
        double per2 = 0.7;
        Pen pen = new Pen(Color.Pink);
        int n = 10;
        double leng = 100.0;


        void drawCayleyTree(int n,
                double x0, double y0, double leng, double th)
        {
            if (n == 0) return;

            double x1 = x0 + leng * Math.Cos(th);
            double y1 = y0 + leng * Math.Sin(th);

            drawLine(pen, x0, y0, x1, y1);

            drawCayleyTree(n - 1, x1, y1, per1 * leng, th + th1);
            drawCayleyTree(n - 1, x1, y1, per2 * leng, th - th2);
        }
        void drawLine(Pen pen, double x0, double y0, double x1, double y1)
        {
            graphics.DrawLine(
                pen,
                (int)x0, (int)y0, (int)x1, (int)y1);
        }

        private void btnPenColor_Click(object sender, EventArgs e)
        {
            ColorDialog dialog = new ColorDialog();
            dialog.ShowDialog();
            Color color = dialog.Color;
            pen.Color = color;
        }

        private void numN_ValueChanged(object sender, EventArgs e)
        {
            n = (int)numN.Value;
        }

        private void tbxLeng_TextChanged(object sender, EventArgs e)
        {
            leng = Convert.ToDouble(tbxLeng.Text.ToString());
        }

        private void tbxPer1_TextChanged(object sender, EventArgs e)
        {
            if (tbxPer1.Text.ToString() != "")
            {
                per1 = Convert.ToDouble(tbxPer1.Text.ToString());
            };
        }

        private void tbxPer2_TextChanged(object sender, EventArgs e)
        {
            if (tbxPer2.Text.ToString() != "")
            {
                per2 = Convert.ToDouble(tbxPer2.Text.ToString());
            };
        }

        private void tbxTh1_TextChanged(object sender, EventArgs e)
        {
            if (tbxTh1.Text.ToString() != "")
            {
                th1 = Convert.ToDouble(tbxTh1.Text.ToString()) * Math.PI / 180;

            };
        }

        private void tbxTh2_TextChanged(object sender, EventArgs e)
        {
            if (tbxTh2.Text.ToString() != "")
            {
                th2 = Convert.ToDouble(tbxTh2.Text.ToString()) * Math.PI / 180;

            };
        }
    }
}
